home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / chrome / calendar.jar / content / calendar / printDialog.js < prev    next >
Text File  |  2007-08-17  |  10KB  |  244 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is OEone Calendar Code, released October 31st, 2001.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * OEone Corporation.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s): Garth Smedley <garths@oeone.com>
  22.  *                 Mike Potter <mikep@oeone.com>
  23.  *                 Colin Phillips <colinp@oeone.com> 
  24.  *                 Chris Charabaruk <ccharabaruk@meldstar.com>
  25.  *                 ArentJan Banck <ajbanck@planet.nl>
  26.  *                 Chris Allen
  27.  *                 Eric Belhaire <belhaire@ief.u-psud.fr>
  28.  *                 Michiel van Leeuwen <mvl@exedo.nl>
  29.  *                 Matthew Willis <mattwillis@gmail.com>
  30.  *
  31.  * Alternatively, the contents of this file may be used under the terms of
  32.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  33.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  34.  * in which case the provisions of the GPL or the LGPL are applicable instead
  35.  * of those above. If you wish to allow use of your version of this file only
  36.  * under the terms of either the GPL or the LGPL, and not to allow others to
  37.  * use your version of this file under the terms of the MPL, indicate your
  38.  * decision by deleting the provisions above and replace them with the notice
  39.  * and other provisions required by the GPL or the LGPL. If you do not delete
  40.  * the provisions above, a recipient may use your version of this file under
  41.  * the terms of any one of the MPL, the GPL or the LGPL.
  42.  *
  43.  * ***** END LICENSE BLOCK ***** */
  44.  
  45. var gPrintSettings = null;
  46.  
  47. function getCalendarView()
  48. {
  49.     var theView = window.opener.currentView();
  50.     if (!theView.startDay) {
  51.         theView = null;
  52.     }
  53.     return theView;
  54. }
  55.  
  56. function loadCalendarPrintDialog()
  57. {
  58.     // set the datepickers to the currently selected dates
  59.     var theView = getCalendarView();
  60.     if (theView) {
  61.         document.getElementById("start-date-picker").value = theView.startDay.jsDate;
  62.         document.getElementById("end-date-picker").value = theView.endDay.jsDate;
  63.     } else {
  64.         document.getElementById("printCurrentViewRadio").setAttribute("disabled", true);
  65.     }
  66.     if (!theView || !theView.getSelectedItems({}).length) {
  67.         document.getElementById("selected").setAttribute("disabled", true);
  68.     }
  69.     document.getElementById(theView ? "printCurrentViewRadio" : "custom-range")
  70.             .setAttribute("selected", true);
  71.  
  72.     // Get a list of formatters
  73.     var contractids = new Array();
  74.     var catman = Components.classes["@mozilla.org/categorymanager;1"]
  75.                            .getService(Components.interfaces.nsICategoryManager);
  76.     var catenum = catman.enumerateCategory('cal-print-formatters');
  77.  
  78.     // Walk the list, adding items to the layout menupopup
  79.     var layoutList = document.getElementById("layout-field");
  80.     while (catenum.hasMoreElements()) {
  81.         var entry = catenum.getNext();
  82.         entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
  83.         var contractid = catman.getCategoryEntry('cal-print-formatters', entry);
  84.         var formatter = Components.classes[contractid]
  85.                                   .getService(Components.interfaces.calIPrintFormatter);
  86.         // Use the contractid as value
  87.         layoutList.appendItem(formatter.name, contractid);
  88.     }
  89.     layoutList.selectedIndex = 0;
  90.  
  91.     opener.setCursor("auto");
  92.  
  93.     refreshHtml();
  94.  
  95.     self.focus();
  96. }
  97.  
  98. /**
  99.  * Gets the settings from the dialog's UI widgets.
  100.  * notifies an Object with title, layoutCId, eventList, start, and end
  101.  *          properties containing the appropriate values.
  102.  */
  103. function getEventsAndDialogSettings(receiverFunc)
  104. {
  105.     var settings = new Object();
  106.     var tempTitle = document.getElementById("title-field").value;
  107.     settings.title = (tempTitle || calGetString("calendar", "Untitled"));
  108.     settings.layoutCId = document.getElementById("layout-field").value;
  109.     settings.start = null;
  110.     settings.end = null;
  111.     settings.eventList = null;
  112.  
  113.     var theView = getCalendarView();
  114.     switch (document.getElementById("view-field").selectedItem.value) {
  115.         case 'currentview':
  116.         case '': //just in case
  117.             settings.start = theView.startDay;
  118.             settings.end   = theView.endDay;
  119.             break;
  120.         case 'selected':
  121.             settings.eventList = theView.getSelectedItems({});
  122.             break;
  123.         case 'custom':
  124.             // We return the time from the timepickers using the selected
  125.             // timezone, as not doing so in timezones with a positive offset
  126.             // from UTC may cause the printout to include the wrong days.
  127.             var currentTimezone = calendarDefaultTimezone();
  128.             settings.start = jsDateToDateTime(document.getElementById("start-date-picker").value);
  129.             settings.start = settings.start.getInTimezone(currentTimezone);
  130.             settings.end   = jsDateToDateTime(document.getElementById("end-date-picker").value);
  131.             settings.end   = settings.end.getInTimezone(currentTimezone);
  132.             break ;
  133.         default :
  134.             dump("Error : no case in printDialog.js::printCalendar()");
  135.     }
  136.  
  137.     if (settings.eventList) {
  138.         receiverFunc(settings);
  139.     } else {
  140.         // end isn't exclusive, so we need to add one day
  141.         settings.end = settings.end.clone();
  142.         settings.end.day = settings.end.day + 1;
  143.         settings.eventList = [];
  144.         var listener = {
  145.             onOperationComplete:
  146.             function onOperationComplete(aCalendar, aStatus, aOperationType, aId, aDateTime) {
  147.                 receiverFunc(settings);
  148.             },
  149.             onGetResult:
  150.             function onGetResult(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
  151.                 settings.eventList = settings.eventList.concat(aItems);
  152.             }
  153.         };
  154.         window.opener.getCompositeCalendar().getItems(
  155.             Components.interfaces.calICalendar.ITEM_FILTER_TYPE_EVENT | 
  156.             Components.interfaces.calICalendar.ITEM_FILTER_CLASS_OCCURRENCES,
  157.             0, settings.start, settings.end, listener);
  158.     }
  159. }
  160.  
  161. /**
  162.  * Looks at the selections the user has made (start date, layout, etc.), and
  163.  * updates the HTML in the iframe accordingly. This is also called when a
  164.  * dialog UI element has changed, since we'll want to refresh the preview.
  165.  */
  166. function refreshHtml(finishFunc)
  167. {
  168.     getEventsAndDialogSettings(
  169.         function getEventsAndDialogSettings_response(settings) {
  170.             // calGetString can't do "formatStringFromName".
  171.             var sbs = Components.classes["@mozilla.org/intl/stringbundle;1"]
  172.                                 .getService(Components.interfaces.nsIStringBundleService);
  173.             var props = sbs.createBundle("chrome://calendar/locale/calendar.properties");
  174.             document.title = props.formatStringFromName("PrintPreviewWindowTitle", [settings.title], 1);
  175.  
  176.             var printformatter = Components.classes[settings.layoutCId]
  177.                                            .createInstance(Components.interfaces.calIPrintFormatter);
  178.  
  179.             var html = "";
  180.             try {
  181.                 var pipe = Components.classes["@mozilla.org/pipe;1"]
  182.                                      .createInstance(Components.interfaces.nsIPipe);
  183.                 const PR_UINT32_MAX = 4294967295; // signals "infinite-length"
  184.                 pipe.init(true, true, 0, PR_UINT32_MAX, null);
  185.                 printformatter.formatToHtml(pipe.outputStream,
  186.                                             settings.start,
  187.                                             settings.end,
  188.                                             settings.eventList.length,
  189.                                             settings.eventList,
  190.                                             settings.title);
  191.                 pipe.outputStream.close();
  192.                 // convert byte-array to UTF-8 string:
  193.                 var convStream =
  194.                     Components.classes["@mozilla.org/intl/converter-input-stream;1"]
  195.                               .createInstance(Components.interfaces.nsIConverterInputStream);
  196.                 convStream.init(pipe.inputStream, "UTF-8", 0,
  197.                                 Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  198.                 try {
  199.                     var portion = {};
  200.                     while (convStream.readString(-1, portion)) {
  201.                         html += portion.value;
  202.                     }
  203.                 } finally {
  204.                     convStream.close();
  205.                 }
  206.             } catch (e) {
  207.                 dump("printDialog::refreshHtml:" + e + "\n");
  208.                 Components.utils.reportError(e);
  209.             }
  210.             var iframeDoc = document.getElementById("content").contentDocument;
  211.             iframeDoc.documentElement.innerHTML = html;
  212.             iframeDoc.title = settings.title;
  213.  
  214.             if (finishFunc) {
  215.                 finishFunc();
  216.             }
  217.         }
  218.     );
  219. }
  220.  
  221. function printAndClose()
  222. {
  223.     refreshHtml(
  224.         function finish() {
  225.             PrintUtils.print();
  226.             var closeDialog = true;
  227. //@line 232 "/cygdrive/c/builds/tinderbox/Lt-Mozilla1.8/WINNT_5.2_Depend/mozilla/calendar/resources/content/printDialog.js"
  228.             // XXX: printing fails "printing failed while in preview"
  229.             //      if dialog is closed too early on Unix
  230.             if (closeDialog) {
  231.                 document.getElementById("calendar-new-printwindow").cancelDialog();
  232.             }
  233.         });
  234.     return false; // leave open
  235. }
  236.  
  237. /**
  238.  * Called when once a date has been selected in the datepicker.
  239.  */
  240. function onDatePick() {
  241.     calRadioGroupSelectItem("view-field", "custom-range");
  242.     refreshHtml();
  243. }
  244.